using FluentValidation.AspNetCore;
using Microsoft.OpenApi.Models;
using Sckf.MicroService.Services.Catalog.Infrastructure;
using Sckf.MicroService.Services.Catalog.Infrastructure.Repositories;
using Masa.Contrib.Dispatcher.IntegrationEvents.EventLogs.EFCore;
using Sckf.MicroService.Services.Catalog.Application.Middleware;
using Masa.BuildingBlocks.Data.UoW;
using Masa.BuildingBlocks.Extensions.BackgroundJobs;
using Hangfire;
using Hangfire.MySql;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

// add validation
builder.Services.AddFluentValidationAutoValidation();

// add snowflake id
builder.Services.AddSnowflake();

// add mapping
builder.Services.AddMapster();

// add backgroundjob
builder.Services.AddBackgroundJob(options =>
 {
     options.UseHangfire(configuration =>
     {
         configuration.UseStorage(new MySqlStorage(
            @"server=192.168.0.10;user=root;password=Jykj000000;database=db_sckfmicroservice_hangfire;allowuservariables=True;",
            new MySqlStorageOptions()));
     });
 });

// add eventbus
builder.Services.AddEventBus();

// add db repository
builder.Services.AddScoped<ICatalogRepository, CatalogRepository>()
    .AddMasaDbContext<CatalogDbContext>(dbOptions => dbOptions.UseMySQL("server=192.168.0.10;database=db_sckfmicroservice;uid=root;pwd=Jykj000000"))
    //.AddIntegrationEventBus<IntegrationEventLogService>(options =>
    //{
    //    options//.UseDapr()
    //            .UseEventLog<CatalogDbContext>()
    //            .UseEventBus(eventBusBuilder => eventBusBuilder.UseMiddleware(typeof(ValidatorMiddleware<>)))
    //            .UseUoW<CatalogDbContext>();
    //})
    ;

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(options =>
{
    options.SwaggerDoc("v1", new OpenApiInfo
    {
        Title = "Sckf MicroService - Catalog HTTP API",
        Version = "v1",
        Description = "The Catalog Service HTTP API"
    });
});

var app = builder.AddServices(options => { 
    options.Version = "v1.1";
});

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHangfireDashboard();

app.UseHttpsRedirection();

app.Run();
